﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace Afru
{
    public class LineBatch
    {
        #region Variables
                
        Texture2D _empty_texture;
        bool _set_data = false;

        #endregion

        public LineBatch(GraphicsDevice device)
        {
            _empty_texture = new Texture2D(device, 1, 1, true, SurfaceFormat.Color);
        }

        public void DrawLine(SpriteBatch batch, Color color, Vector2 point1, Vector2 point2)
        {
            DrawLine(batch, color, point1, point2, 0);
        }

        /// <summary>
        /// Draws the bounds of a rectangle to the active SpriteBatch
        /// </summary>
        /// <param name="rectangle">The rectangle to draw</param>
        /// <param name="LineColour">The colour to draw the boundary with</param>
        public void DrawBounds(SpriteBatch batch, Rectangle rectangle, Color LineColour)
        {
            // The top of the rectangle
            DrawLine(batch, LineColour, new Vector2(rectangle.Left, rectangle.Top), new Vector2(rectangle.Right, rectangle.Top));
            // The bottom of the rectangle
            DrawLine(batch, LineColour, new Vector2(rectangle.Left, rectangle.Bottom), new Vector2(rectangle.Right, rectangle.Bottom));
            // The left side of the rectangle
            DrawLine(batch, LineColour, new Vector2(rectangle.Left, rectangle.Top), new Vector2(rectangle.Left, rectangle.Bottom));
            // The right side of the rectangle
            DrawLine(batch, LineColour, new Vector2(rectangle.Right, rectangle.Top), new Vector2(rectangle.Right, rectangle.Bottom));

        }

        /// <summary>
        /// Draws the bounds of a rotated rectangle to the active SpriteBatch
        /// </summary>
        /// <param name="rectangle">The rotated rectangle to draw</param>
        /// <param name="LineColour">The colour to draw the rectangle with</param>
        public void DrawBounds(SpriteBatch batch, RotatedRectangle rectangle, Color LineColour)
        {
            // The top of the rectangle
            DrawLine(batch, LineColour, rectangle.UpperLeftCorner(), rectangle.UpperRightCorner());
            // The bottom of the rectangle
            DrawLine(batch, LineColour, rectangle.LowerLeftCorner(), rectangle.LowerRightCorner());
            // The left side of the rectangle
            DrawLine(batch, LineColour, rectangle.UpperLeftCorner(), rectangle.LowerLeftCorner());
            // The right side of the rectangle
            DrawLine(batch, LineColour, rectangle.UpperRightCorner(), rectangle.LowerRightCorner());
        }

        /// <summary>
        /// Draw a line into a SpriteBatch
        /// </summary>
        /// <param name="batch">SpriteBatch to draw line</param>
        /// <param name="color">The line color</param>
        /// <param name="point1">Start Point</param>
        /// <param name="point2">End Point</param>
        /// <param name="Layer">Layer or Z position</param>
        public void DrawLine(SpriteBatch batch, Color color, Vector2 point1, Vector2 point2, float Layer)
        {
            //Check if data has been set for texture
            //Do this only once otherwise
            if (!_set_data)
            {
                _empty_texture.SetData(new[] { Color.White });
                _set_data = true;
            }


            float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
            float length = (point2 - point1).Length();

            batch.Draw(_empty_texture, point1, null, color,
                       angle, Vector2.Zero, new Vector2(length, 1),
                       SpriteEffects.None, Layer);
        }
    }
}
